package mes.kanban.controller;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mes.kanban.service.MesJobServiceI;
import mes.kanban.util.PageBean;
import mes.kanban.vo.AjaxReturnVO;
import mes.kanban.vo.MesJobVO;


/**
 * 角色作业管理
 * @author zongrui.liu
 *
 */
@Controller
@RequestMapping(value="/userJob")
public class UserJobController implements Serializable{

	private static final long serialVersionUID = 2131046413561732053L;
	
	private static final Logger log = LoggerFactory.getLogger(UserJobController.class);
	
	
	@Autowired
	private MesJobServiceI mesJobServiceI;
	
	/**
	 * 跳转到角色管理页面
	 * @return
	 */
	@GetMapping(value="/toList")
	public String gotoRoleManage() {
		log.info("作业管理界面");
		return "/user/job";
	}
	
	/**
	 * 分页查询作业列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/list")
	@ResponseBody
	public AjaxReturnVO getJobsForBean(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
		
		log.info("分页查询角色列表信息");
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			PageBean pb = this.mesJobServiceI.getJobsForBean(pageNum,pageSize);
			vo.setData(pb);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("报错了");
		}
		
		return vo;
	}
	
	/**
	 * 查询作业列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	/*@GetMapping(value="/getJobs")
	@ResponseBody
	public AjaxReturnVO getJobs() {
		
		log.info("查询角色列表信息");
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			List<MesJobVO> jobList = this.mesJobServiceI.getJobs();
			vo.setData(jobList);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("查询出错");
		}
		
		return vo;
	}*/
	
	/**
	 * 添加作业信息
	 * @param vo
	 * @return
	 */
    @PostMapping("/add")
    @ResponseBody
    public AjaxReturnVO add(@RequestBody MesJobVO vo) {
    	log.info("添加角色配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
			this.mesJobServiceI.save(vo);
	    	ajaxVo.setData(vo);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }

    /**
     * 编辑作业信息
     * @param vo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxReturnVO edit(@RequestBody MesJobVO vo) {
    	log.info("修改角色配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
			this.mesJobServiceI.edit(vo);
	    	ajaxVo.setData(vo);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }
    
    /**
     * 删除作业
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public AjaxReturnVO deleteById(@RequestParam("id") String id) {
    	AjaxReturnVO j = new AjaxReturnVO();
    	try {
	    	this.mesJobServiceI.deleteJobById(id);
	    	j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
    	return j;
    }
    
    
	
}
